// import type { Plugin } from 'vue'

import md5 from 'js-md5'
import { App } from 'vue'
import { loginStateStoreWithOut } from '@/store/modules/loginState'
/**
 *
 * @param component 需要注册的组件
 * @param alias 组件别名
 * @returns any
 */
export const withInstall = <T>(component: T, alias?: string) => {
  const comp = component as any
  comp.install = (app: any) => {
    app.component(comp.name || comp.displayName, component)
    if (alias) {
      app.config.globalProperties[alias] = component
    }
  }
  return component as T & Plugin
}
export const encryptPwd = (pwd: string): string => {
  return md5(pwd + 'wedonekRpc_6xy')
}
/**
 * @param str 需要转下划线的驼峰字符串
 * @returns 字符串下划线
 */
export const humpToUnderline = (str: string): string => {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase()
}

/**
 * @param str 需要转驼峰的下划线字符串
 * @returns 字符串驼峰
 */
export const underlineToHump = (str: string): string => {
  if (!str) return ''
  return str.replace(/\-(\w)/g, (_, letter: string) => {
    return letter.toUpperCase()
  })
}

/**
 * 驼峰转横杠
 */
export const humpToDash = (str: string): string => {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase()
}

export const setCssVar = (prop: string, val: any, dom = document.documentElement) => {
  dom.style.setProperty(prop, val)
}

export const getCssVar = (prop: string, dom = document.documentElement) => {
  return getComputedStyle(dom).getPropertyValue(prop)
}

/**
 * 查找数组对象的某个下标
 * @param {Array} ary 查找的数组
 * @param {Functon} fn 判断的方法
 */
// eslint-disable-next-line
export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
  if (ary.findIndex) {
    return ary.findIndex(fn)
  }
  let index = -1
  ary.some((item: T, i: number, ary: Array<T>) => {
    const ret: T = fn(item, i, ary)
    if (ret) {
      index = i
      return ret
    }
  })
  return index
}

export const trim = (str: string) => {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}

/**
 * @param {Date | number | string} time 需要转换的时间
 * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
 */
export function formatTime(time: Date | number | string, fmt: string) {
  if (!time) return ''
  else {
    const date = new Date(time)
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'H+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      S: date.getMilliseconds()
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
        )
      }
    }
    return fmt
  }
}
export const joinStr = (
  array: Array<Object> | null,
  func: (item: Object) => string,
  separator: string = ','
) => {
  if (array == null || array.length == 0) return ''
  let str = ''
  array.forEach((c) => {
    str += separator + func(c)
  })
  return str.substring(0)
}

/**
 * 生成随机字符串
 */
export function toAnyString() {
  const str: string = 'xxxxx-xxxxx-4xxxx-yxxxx-xxxxx'.replace(/[xy]/g, (c: string) => {
    const r: number = (Math.random() * 16) | 0
    const v: number = c === 'x' ? r : (r & 0x3) | 0x8
    return v.toString()
  })
  return str
}

/**
 * 首字母大写
 */
export function firstUpperCase(str: string) {
  return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
}

/**
 * 把对象转为formData
 */
export function objToFormData(obj: Recordable) {
  const formData = new FormData()
  Object.keys(obj).forEach((key) => {
    formData.append(key, obj[key])
  })
  return formData
}
export function formatFileUri(src: string) {
  if (src == null) {
    return null
  } else if (src.indexOf('/file/') === -1) {
    return src
  } else if (src.indexOf('accreditId=') !== -1) {
    return src
  }
  const token = loginStateStoreWithOut().token
  if (token) {
    if (src.indexOf('?') === -1) {
      return src + '?accreditId=' + token
    }
    return src + '&accreditId=' + token
  }
}
export function IsNull(str: string) {
  return str == null || str == ''
}

export function GetValueOrDef(str: string, def: string) {
  return str == null || str == '' ? def : str
}
export function OrderBy(array: Array<any>, attr: string) {
  const list = [...array]
  list.sort(function (a, b) {
    let one = a[attr]
    let two = b[attr]
    if (Number.isInteger(one) || Number.isInteger(two)) {
      if (one == null) {
        one = Number.MAX_VALUE
      }
      if (two == null) {
        two = Number.MAX_VALUE
      }
      if (one === two) {
        return 0
      }
      return one > two ? 1 : -1
    } else {
      const x = a.type.toLowerCase()
      const y = b.type.toLowerCase()
      if (x < y) {
        return -1
      }
      if (x > y) {
        return 1
      }
      return 0
    }
  })
  return list
}
export const regTools = (app: App<Element>) => {
  app.config.globalProperties.$formatTime = formatTime
  app.config.globalProperties.$formatFileUri = formatFileUri
  app.config.globalProperties.$isNull = IsNull
  app.config.globalProperties.$getVlaueOrDef = GetValueOrDef
}
